<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <link rel="stylesheet" href="/static/layui/css/layui.css">
</head>

<body>
  <div style="padding: 10px;">
    <div class="breadcrumb-nav">
      <span class="layui-breadcrumb">
        <a href="/dashBoard">首页</a>
        <a href="/itemLists">商品列表</a>
        <a><cite>增加商品</cite></a>
      </span>
    </div>
  </div>
  <form class="layui-form" action="" style="max-width:800px;margin-top:20px">
    <div class="layui-form-item">
      <label class="layui-form-label">商品名称</label>
      <div class="layui-input-block">
        <input type="text" name="itemName" required lay-verify="required" placeholder="请输入商品名称" autocomplete="off"
          class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">商品价格</label>
      <div class="layui-input-block">
        <input type="number" min="0" name="itemPrice" required lay-verify="required" placeholder="请输入商品价格"
          autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">商品库存</label>
      <div class="layui-input-block">
        <input type="number" min="0" name="store" required lay-verify="required" placeholder="请输入商品价格"
          autocomplete="off" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item layui-form-text">
      <label class="layui-form-label">描述</label>
      <div class="layui-input-block">
        <textarea name="desc" placeholder="请输入内容" class="layui-textarea"></textarea>
      </div>
    </div>
    <div class="layui-form-item layui-form-text">
      <label class="layui-form-label">商品封面</label>
      <div class="layui-input-block">
        <button type="button" class="layui-btn" id="upload">
          <i class="layui-icon">&#xe67c;</i>选择图片
        </button>
        <a id="uploadBtn" class="layui-btn layui-btn-warm">点击上传</a>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">商品详情</label>
      <div class="layui-input-block">
       <!-- 富文本编辑器容器 -->
        <div id="content"></div>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-input-block">
        <button class="layui-btn" lay-submit lay-filter="formDemo">立即提交</button>
        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
      </div>
    </div>
  </form>
  <script src="/static/layui/layui.js"></script>
  <script src="/static/js/wangEditor.js"></script>
  <script>
    layui.use(['form', 'jquery', 'layer', 'upload'], function () {
      var form = layui.form;
      var $ = layui.jquery;
      var layer = layui.layer;
      var upload = layui.upload;
      // 商品封面图片上传
      var thumb = '';
      //执行实例
      var uploadInst = upload.render({
        elem: '#upload' //绑定元素
        , url: '/upload' //上传接口
        ,field: 'file'
        ,auto: false
        ,bindAction: '#uploadBtn'
        , done: function (res) {
          //上传完毕回调
          ljkbhhbayer.msg(res.msg, {icon:1
          })
          thumb = res.data.url;
        }
        , error: function () {
          //请求异常回调
          console.log('上传失败');
        }
      });

      // 初始化富文本编辑器
      const E = window.wangEditor
      // 初始化富文本实例
      const editor = new E("#content");
      // 调用create 绘制编辑器
      editor.create()

      //提交
      form.on('submit(formDemo)', function (data) {
        var formData = {
          ...data.field,
          thumb: thumb,
          content: editor.txt.html()
        };
        $.ajax({
          url: '/itemAdd',
          type: 'post',
          data: formData,
          success: function (res) {
            if (res.code === 0) {
              layer.msg(res.msg, {
                icon: 1,
                time: 2000
              }, function () {
                location.href = "/itemLists"
              })
            } else {
              layer.msg(res.msg, {
                icon: 2,
                time: 2000
              })
            }
          }
        })
        return false;
      });
    })
  </script>
</body>

</html>